package com.dp.mallchat.common.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dp.mallchat.common.common.domain.vo.resp.ApiResult;
import com.dp.mallchat.common.common.utils.AssertUtil;
import com.dp.mallchat.common.user.dao.UserEmojiDao;
import com.dp.mallchat.common.user.domain.entity.UserEmoji;
import com.dp.mallchat.common.user.domain.user.req.UserEmojiDeleteReq;
import com.dp.mallchat.common.user.domain.user.req.UserEmojiSaveReq;
import com.dp.mallchat.common.user.domain.user.resp.UserEmojiResp;
import com.dp.mallchat.common.user.mapper.UserEmojiMapper;
import com.dp.mallchat.common.user.service.UserEmojiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-18  10:20
 * @Description: 表情包服务
 */
@Service
@Slf4j
public class UserEmojiServiceImpl extends ServiceImpl<UserEmojiMapper, UserEmoji> implements UserEmojiService {

    @Autowired
    private UserEmojiDao userEmojiDao;

    @Override
    public List<UserEmojiResp> listEmoji(Long uid) {
        return userEmojiDao.listEmojiByUid(uid);
    }

    @Override
    public void saveEmoji(Long uid, UserEmojiSaveReq req) {
        //每个用户最多保存100个表情包
        Integer count = userEmojiDao.lambdaQuery().eq(UserEmoji::getUid, uid).count();
        AssertUtil.isFalse(count > 100, "最多只能保存100个表情包哦~~");

        //是否存在表情包
        UserEmoji userEmoji = userEmojiDao
                .lambdaQuery()
                .eq(UserEmoji::getUid, uid)
                .eq(UserEmoji::getExpressionUrl, req.getExpressionUrl()).one();
        AssertUtil.isEmpty(userEmoji, "该表情包已存在哦~~");

        //保存
        UserEmoji emoji = UserEmoji.builder().uid(uid).expressionUrl(req.getExpressionUrl()).build();
        userEmojiDao.save(emoji);
    }

    @Override
    public void deleteEmoji(Long uid, UserEmojiDeleteReq req) {
        UserEmoji emoji = userEmojiDao.getById(req.getId());
        AssertUtil.isNotEmpty(emoji, "该表情包不存在哦~~");
        AssertUtil.equal(emoji.getUid(), uid, "该表情包不属于你哦~~");
        userEmojiDao.removeById(req.getId());
    }
}
